<?php
/**
 * Created by PhpStorm.
 * User: kb293
 * Date: 2017/7/28
 * Time: 17:54
 */
namespace soa\image;

use util\qrcode\QrCodeUtil;

class ImageQrcode {
    // 二维码保存图片格式
    const QRCODE_IMG_TYPE = 'png';

    // mime 对应的图片类型
    private $mime_type = [
        'image/png' => 'png',
        'image/jpeg' => 'jpeg'
    ];
    /**
     * 创建快递员二维码
     * @param $text     需要生成二维码的字符串
     * @param $file_path 文件名
     * @param string    $logo_url  图片地址
     * @param bool $is_image
     * @return resource|void
     */
    public function createQrCode($text, $file_path, $logo_url = '', $is_image = false) {
        $logo_rs = $this->getImageInfo($logo_url);
        $old_qrcode = QrCodeUtil::getQrCode()->png($text, false, $ec_level = 'H', $size = 10, $margin = 1, false, true);
        $this->embedImage($old_qrcode, $logo_rs ? $this->handleAvatarImage($logo_rs) : $this->getDefaultMosaicImage(), $file_path, $is_image);
    }

    /**
     * 创建快递员二维码 ( 避免影响已有方法 新建方法 )
     * @param $text     需要生成二维码的字符串
     * @param $file_path 文件名
     * @param string    $logo_url  图片地址
     * @param bool $is_image
     * @return resource|void
     */
    public function createQrCodeNew($text, $file_path, $logo_url = '', $is_image = false) {
        $logo_rs = $this->getImageInfo($logo_url);
        $old_qrcode = QrCodeUtil::getQrCode()->png($text, false, $ec_level = 'H', $size = 10, $margin = 1, false, true);
        $this->embedImageNew($old_qrcode, $logo_rs ? $this->handleAvatarImage($logo_rs) : $this->getDefaultMosaicImage(), $file_path, $is_image);
    }

    /**
     * 获取图片的宽高
     *
     * @param $imgurl   图片路径
     * @return array
     */
    private function getSize($imgurl) {
        list($img_w, $img_h, $img_t) = getimagesize($imgurl);
        return compact('img_w', 'img_h', 'img_t');
    }

    /**
     * 获取资源图片的宽高
     * @param $img_res
     * @return array
     */
    private function getSizeByResoure($img_res) {
        $img_w = imagesx($img_res);
        $img_h = imagesy($img_res);
        return compact('img_w', 'img_h');
    }

    private function getImageInfo($image_src) {
        return @file_get_contents($image_src);
    }

    /**
     * 创建背板(做背景)
     * @param $width
     * @param $height
     * @param $rgb
     * @return resource
     */
    public function createBack($width, $height, $rgb) {
        $back = imagecreatetruecolor($width, $height);
        $color = imagecolorallocate($back, $rgb[0], $rgb[1], $rgb[2]);
        imagefill($back, 0, 0, $color);
        return $back;
    }

    /**
     * 处理用户头像
     * 将压缩比例然后放在边框10px的面板上面
     * @param string $avatar_path
     * @param int $qrcode_img_width
     * @return resource
     */
    public function handleAvatarImage($logo_rs, $qrcode_img_width = 516) {
        $mosaic_size = round($qrcode_img_width / 3.685, 0); // logo 大小 140px
        $border_thick = round($mosaic_size * 0.0714, 0); // logo 边框厚度 10px
        $avatar_size = $mosaic_size - $border_thick; // 指定头像大小 130px
        // 创建画布
        $back = $this->createBack($mosaic_size, $mosaic_size, $rgb = [255, 255, 255]);

        // 将Logo转成资源
        $avatar_rs = imagecreatefromstring($logo_rs);
        // 获取图片基本信息
        $avatar_mime = $this->getSizeByResoure($avatar_rs);
        // 创建画布
        $avatar_dts = imagecreatetruecolor($avatar_size, $avatar_size);
        // 将图片压缩成 $avatar_size
        imagecopyresampled($avatar_dts, $avatar_rs, 0, 0, 0, 0,  $avatar_size, $avatar_size, $avatar_mime['img_w'], $avatar_mime['img_h']);
        // 将Logo贴在背板上
        imagecopy($back, $avatar_dts, $border_thick/2, $border_thick/2, 0, 0, $avatar_size, $avatar_size);

        imagedestroy($avatar_dts);
        imagedestroy($avatar_rs);

        return $back;
    }

    /**
     * 嵌入 logo 图像到 qrcode 图像
     * @param $image_dst resource qrcode
     * @param $image_src resource logo
     * @param $file_path
     * @param $is_image
     */
    private function embedImage($image_dst, $image_src, $file_path, $is_image)
    {
        if (!is_resource($image_src)) {
            $image_src = $this->createResource($image_src);
        }

        if (!is_resource($image_dst)) {
            $image_dst = $this->createResource($image_dst);
        }

        $src_size = $this->getSizeByResoure($image_src);
        $dst_size = $this->getSizeByResoure($image_dst);

        $embed_position = $this->calcPos($dst_size['img_w'], $dst_size['img_h'], $src_size['img_w'], $src_size['img_h']);

        imagesavealpha($image_dst, true);

        $image = imagecreatetruecolor($dst_size['img_w'], $dst_size['img_h']);

        imagecopy($image, $image_dst, 0, 0, 0, 0, $dst_size['img_w'], $dst_size['img_h']);
        imagecopyresampled($image, $image_src, $embed_position['pos_x'], $embed_position['pos_y'], 0, 0, $embed_position['pos_x'], $embed_position['pos_y'], $src_size['img_w'], $src_size['img_h'] );

        //imagecopy($image_dst, $image_src, $embed_position['pos_x'], $embed_position['pos_y'], 0, 0, $src_size['img_w'], $src_size['img_h']);
        // 将图片资源保存成图片
        if ($is_image) {
            header('Content-Type:image/jpeg');
            imagejpeg($image);
            imagedestroy($image_src);
            imagedestroy($image_dst);
            exit;
        } else
            $this->saveResourceToImage(self::QRCODE_IMG_TYPE, $image, $file_path);

        imagedestroy($image_src);
        imagedestroy($image_dst);
    }

    /**
     * 嵌入 logo 图像到 qrcode 图像 （ 固定比例LOGO ）
     * @param $image_dst resource qrcode
     * @param $image_src resource logo
     * @param $file_path
     * @param $is_image
     */
    private function embedImageNew($image_dst, $image_src, $file_path, $is_image)
    {
        $logo_rate = 0.3; //LOGO大小占二维码比例

        if (!is_resource($image_src)) {
            $image_src = $this->createResource($image_src);
        }

        if (!is_resource($image_dst)) {
            $image_dst = $this->createResource($image_dst);
        }

        $src_size = $this->getSizeByResoure($image_src);
        $dst_size = $this->getSizeByResoure($image_dst);

        $logo_embed_position = $this->calcRatePos($dst_size['img_w'], $dst_size['img_h'],$logo_rate);
        $logo_embed_width = $dst_size['img_w'] * $logo_rate;
        $logo_embed_height = $dst_size['img_h'] * $logo_rate;

        imagesavealpha($image_dst, true);

        $image = imagecreatetruecolor($dst_size['img_w'], $dst_size['img_h']);

        imagecopy($image, $image_dst, 0, 0, 0, 0, $dst_size['img_w'], $dst_size['img_h']);
        imagecopyresampled($image, $image_src, $logo_embed_position['pos_x'], $logo_embed_position['pos_y'], 0, 0, $logo_embed_width, $logo_embed_height, $src_size['img_w'], $src_size['img_h'] );

        //imagecopy($image_dst, $image_src, $embed_position['pos_x'], $embed_position['pos_y'], 0, 0, $src_size['img_w'], $src_size['img_h']);
        // 将图片资源保存成图片
        if ($is_image) {
            header('Content-Type:image/jpeg');
            imagejpeg($image);
            imagedestroy($image_src);
            imagedestroy($image_dst);
            exit;
        } else
            $this->saveResourceToImage(self::QRCODE_IMG_TYPE, $image, $file_path);

        imagedestroy($image_src);
        imagedestroy($image_dst);
    }

    /**
     * 计算原始图像嵌入到目标图像的位置
     * @param $big_x
     * @param $big_y
     * @param $small_x
     * @param $small_y
     * @return array
     */
    private function calcPos($big_x, $big_y, $small_x, $small_y)
    {
        $pos_x = ($big_x - $small_x) / 2;
        $pos_y = ($big_y - $small_y) / 2;
        return compact('pos_x', 'pos_y');
    }

    /** 根据比例计算原始图像嵌入到目标图像的位置
     * @param $big_x
     * @param $big_y
     * @param $logo_rate
     * @return array
     */
    private function calcRatePos($big_x, $big_y,$logo_rate){
        $logo_rate_pose = ( 1 - $logo_rate) / 2;
        $pos_x = $big_x * $logo_rate_pose;
        $pos_y = $big_y * $logo_rate_pose;
        return compact('pos_x', 'pos_y');
    }

    /**
     * 从文件创建图像资源
     * @param $image_path
     * @return mixed
     */
    private function createResource($image_path)
    {
        $size = $this->getSize($image_path);
        if (!in_array($size['img_t'], $this->mime_type))
            throw new Exception('图片类型不对');

        $fn = imagecreatefrom . $this->mime_type[$size['img_t']];
        return $fn($image_path);
    }

    /**
     * 从图像资源专成图片的方法
     * @param $image_type
     * @param $resource
     * @param $filepath
     * @return string
     */
    private function saveResourceToImage($image_type, $resource, $filepath)
    {
        $fn = 'image' . $image_type;
        return $fn($resource, $filepath);
    }

    /**
     *  生成二维码
     */
    public function generateQrCode($text, $filename, $ec_level, $size, $margin) {
        return QrCodeUtil::getQrCode()->png($text, $filename, $ec_level, $size, $margin);
    }

    /**
     * 从字符串获取嵌入的图像
     * @param $data
     * @return resource
     * @throws \Exception
     */
    private function getMosaicImageByData($data)
    {
        return imagecreatefromstring(base64_decode($data));
    }

    /**
     * 获取默认的嵌入图像资源
     *
     * 微快递 LOGO
     * @return resource
     * @throws \Exception
     */
    private function getDefaultMosaicImage()
    {
        $data = "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
        jULlO8P9/i2rQySuZZQAAAABJRU5ErkJggg==";

        return $this->getMosaicImageByData($data);
    }

    /**
     * 获取边框资源
     * @return resource
     */
    private function getBorderResource()
    {
        $data = "iVBORw0KGgoAAAANSUhEUgAAAIwAAACMCAYAAACuwEE+AAAACXBIWXMAAAsTAAALEwEAmpwYAAAKTWlDQ1BQaG90b3Nob3AgSUNDIHB
        yb2ZpbGUAAHjanVN3WJP3Fj7f92UPVkLY8LGXbIEAIiOsCMgQWaIQkgBhhBASQMWFiApWFBURnEhVxILVCkidiOKgKLhnQYqIWotVXDjuH9yntX1
        67+3t+9f7vOec5/zOec8PgBESJpHmomoAOVKFPDrYH49PSMTJvYACFUjgBCAQ5svCZwXFAADwA3l4fnSwP/wBr28AAgBw1S4kEsfh/4O6UCZXACC
        RAOAiEucLAZBSAMguVMgUAMgYALBTs2QKAJQAAGx5fEIiAKoNAOz0ST4FANipk9wXANiiHKkIAI0BAJkoRyQCQLsAYFWBUiwCwMIAoKxAIi4EwK4
        BgFm2MkcCgL0FAHaOWJAPQGAAgJlCLMwAIDgCAEMeE80DIEwDoDDSv+CpX3CFuEgBAMDLlc2XS9IzFLiV0Bp38vDg4iHiwmyxQmEXKRBmCeQinJeb
        IxNI5wNMzgwAABr50cH+OD+Q5+bk4eZm52zv9MWi/mvwbyI+IfHf/ryMAgQAEE7P79pf5eXWA3DHAbB1v2upWwDaVgBo3/ldM9sJoFoK0Hr5i3k4
        /EAenqFQyDwdHAoLC+0lYqG9MOOLPv8z4W/gi372/EAe/tt68ABxmkCZrcCjg/1xYW52rlKO58sEQjFu9+cj/seFf/2OKdHiNLFcLBWK8ViJuFAi
        Tcd5uVKRRCHJleIS6X8y8R+W/QmTdw0ArIZPwE62B7XLbMB+7gECiw5Y0nYAQH7zLYwaC5EAEGc0Mnn3AACTv/mPQCsBAM2XpOMAALzoGFyolBdM
        xggAAESggSqwQQcMwRSswA6cwR28wBcCYQZEQAwkwDwQQgbkgBwKoRiWQRlUwDrYBLWwAxqgEZrhELTBMTgN5+ASXIHrcBcGYBiewhi8hgkEQcgI
        E2EhOogRYo7YIs4IF5mOBCJhSDSSgKQg6YgUUSLFyHKkAqlCapFdSCPyLXIUOY1cQPqQ28ggMor8irxHMZSBslED1AJ1QLmoHxqKxqBz0XQ0D12A
        lqJr0Rq0Hj2AtqKn0UvodXQAfYqOY4DRMQ5mjNlhXIyHRWCJWBomxxZj5Vg1Vo81Yx1YN3YVG8CeYe8IJAKLgBPsCF6EEMJsgpCQR1hMWEOoJewj
        tBK6CFcJg4Qxwicik6hPtCV6EvnEeGI6sZBYRqwm7iEeIZ4lXicOE1+TSCQOyZLkTgohJZAySQtJa0jbSC2kU6Q+0hBpnEwm65Btyd7kCLKArCCX
        kbeQD5BPkvvJw+S3FDrFiOJMCaIkUqSUEko1ZT/lBKWfMkKZoKpRzame1AiqiDqfWkltoHZQL1OHqRM0dZolzZsWQ8ukLaPV0JppZ2n3aC/pdLoJ
        3YMeRZfQl9Jr6Afp5+mD9HcMDYYNg8dIYigZaxl7GacYtxkvmUymBdOXmchUMNcyG5lnmA+Yb1VYKvYqfBWRyhKVOpVWlX6V56pUVXNVP9V5qgtU
        q1UPq15WfaZGVbNQ46kJ1Bar1akdVbupNq7OUndSj1DPUV+jvl/9gvpjDbKGhUaghkijVGO3xhmNIRbGMmXxWELWclYD6yxrmE1iW7L57Ex2Bfsb
        di97TFNDc6pmrGaRZp3mcc0BDsax4PA52ZxKziHODc57LQMtPy2x1mqtZq1+rTfaetq+2mLtcu0W7eva73VwnUCdLJ31Om0693UJuja6UbqFutt1
        z+o+02PreekJ9cr1Dund0Uf1bfSj9Rfq79bv0R83MDQINpAZbDE4Y/DMkGPoa5hpuNHwhOGoEctoupHEaKPRSaMnuCbuh2fjNXgXPmasbxxirDTe
        ZdxrPGFiaTLbpMSkxeS+Kc2Ua5pmutG003TMzMgs3KzYrMnsjjnVnGueYb7ZvNv8jYWlRZzFSos2i8eW2pZ8ywWWTZb3rJhWPlZ5VvVW16xJ1lzr
        LOtt1ldsUBtXmwybOpvLtqitm63Edptt3xTiFI8p0in1U27aMez87ArsmuwG7Tn2YfYl9m32zx3MHBId1jt0O3xydHXMdmxwvOuk4TTDqcSpw+lX
        ZxtnoXOd8zUXpkuQyxKXdpcXU22niqdun3rLleUa7rrStdP1o5u7m9yt2W3U3cw9xX2r+00umxvJXcM970H08PdY4nHM452nm6fC85DnL152Xlle
        +70eT7OcJp7WMG3I28Rb4L3Le2A6Pj1l+s7pAz7GPgKfep+Hvqa+It89viN+1n6Zfgf8nvs7+sv9j/i/4XnyFvFOBWABwQHlAb2BGoGzA2sDHwSZ
        BKUHNQWNBbsGLww+FUIMCQ1ZH3KTb8AX8hv5YzPcZyya0RXKCJ0VWhv6MMwmTB7WEY6GzwjfEH5vpvlM6cy2CIjgR2yIuB9pGZkX+X0UKSoyqi7q
        UbRTdHF09yzWrORZ+2e9jvGPqYy5O9tqtnJ2Z6xqbFJsY+ybuIC4qriBeIf4RfGXEnQTJAntieTE2MQ9ieNzAudsmjOc5JpUlnRjruXcorkX5unO
        y553PFk1WZB8OIWYEpeyP+WDIEJQLxhP5aduTR0T8oSbhU9FvqKNolGxt7hKPJLmnVaV9jjdO31D+miGT0Z1xjMJT1IreZEZkrkj801WRNberM/Z
        cdktOZSclJyjUg1plrQr1zC3KLdPZisrkw3keeZtyhuTh8r35CP5c/PbFWyFTNGjtFKuUA4WTC+oK3hbGFt4uEi9SFrUM99m/ur5IwuCFny9kLBQ
        uLCz2Lh4WfHgIr9FuxYji1MXdy4xXVK6ZHhp8NJ9y2jLspb9UOJYUlXyannc8o5Sg9KlpUMrglc0lamUycturvRauWMVYZVkVe9ql9VbVn8qF5Vf
        rHCsqK74sEa45uJXTl/VfPV5bdra3kq3yu3rSOuk626s91m/r0q9akHV0IbwDa0b8Y3lG19tSt50oXpq9Y7NtM3KzQM1YTXtW8y2rNvyoTaj9nqd
        f13LVv2tq7e+2Sba1r/dd3vzDoMdFTve75TsvLUreFdrvUV99W7S7oLdjxpiG7q/5n7duEd3T8Wej3ulewf2Re/ranRvbNyvv7+yCW1SNo0eSDpw
        5ZuAb9qb7Zp3tXBaKg7CQeXBJ9+mfHvjUOihzsPcw83fmX+39QjrSHkr0jq/dawto22gPaG97+iMo50dXh1Hvrf/fu8x42N1xzWPV56gnSg98fnk
        gpPjp2Snnp1OPz3Umdx590z8mWtdUV29Z0PPnj8XdO5Mt1/3yfPe549d8Lxw9CL3Ytslt0utPa49R35w/eFIr1tv62X3y+1XPK509E3rO9Hv03/6
        asDVc9f41y5dn3m978bsG7duJt0cuCW69fh29u0XdwruTNxdeo94r/y+2v3qB/oP6n+0/rFlwG3g+GDAYM/DWQ/vDgmHnv6U/9OH4dJHzEfVI0Yj
        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";

        return $this->getMosaicImageByData($data);
    }

    /**
     * 保存文件
     * @param $content  保存的内容
     * @param $to_file  保存的文件名
     * @return mixed
     */
    private function saveToFile($content, $to_file) {
        if ($fp = fopen($to_file, 'w+')) {
            fwrite($fp, $content);
            fclose($fp);
            return $to_file;
        }
        return false;
    }
}